package com.zbkj.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zbkj.common.request.PageParamRequest;
import com.github.pagehelper.PageHelper;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.URLUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zbkj.common.exception.AppException;

import com.zbkj.common.response.CommunityStatRecordDetailResponse;
import com.zbkj.common.result.CommonResultCode;
import com.zbkj.common.utils.CrmebDateUtil;
import com.zbkj.common.model.agent.CommunityStatRecord;
import com.zbkj.service.dao.CommunityStatRecordDao;
import com.zbkj.service.service.CommunityStatRecordService;

import com.zbkj.common.request.CommunityStatRecordSaveRequest;
import com.zbkj.common.request.CommunityStatRecordSearchRequest;
import com.zbkj.common.response.CommunityStatRecordPageDataResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @author Caiyc
* @description CommunityStatRecordServiceImpl 接口实现
* @date 2025-07-21
*/
@Service
public class CommunityStatRecordServiceImpl extends ServiceImpl<CommunityStatRecordDao, CommunityStatRecord> implements CommunityStatRecordService {

    @Resource
    private CommunityStatRecordDao dao;

    @Override
    public Boolean create(CommunityStatRecordSaveRequest communityStatRecordSaveRequest) {
        CommunityStatRecord communityStatRecord = new CommunityStatRecord();
        BeanUtils.copyProperties(communityStatRecordSaveRequest, communityStatRecord, "id");
        boolean save = save(communityStatRecord);
        if (!save) {
            throw new AppException(CommonResultCode.VALIDATE_FAILED, "添加失败");
        }
        return save;
    }

    /**
     * 分页查询小区分红统计排名表
     *
     * @param request          请求参数
     * @param pageParamRequest 分页类参数
     * @return List<CommunityStatRecord>
     * @author Caiyc
     * @since 2025-07-21
     */
    @Override
    public List<CommunityStatRecordPageDataResponse> getList(CommunityStatRecordSearchRequest request, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());

        //带 CommunityStatRecord 类的多条件查询
        LambdaQueryWrapper<CommunityStatRecord> lambdaQueryWrapper = Wrappers.lambdaQuery();
        // type 类型不能为空 bean中已经限制
        /*
        lambdaQueryWrapper.eq(CommunityStatRecord::getType, request.getType());

        if (ObjectUtil.isNotEmpty(request.getId()) && request.getId() > 0) {
            lambdaQueryWrapper.eq(CommunityStatRecord::getId, request.getId());
        }
        if (ObjectUtil.isNotEmpty(request.getName())) {
            lambdaQueryWrapper.like(CommunityStatRecord::getName, URLUtil.decode(request.getName()));
        }

        if (ObjectUtil.isNotEmpty(request.getStatus())) {
            lambdaQueryWrapper.eq(CommunityStatRecord::getStatus, request.getStatus());
        }
        if (ObjectUtil.isNotEmpty(request.getMethod())) {
            lambdaQueryWrapper.eq(CommunityStatRecord::getMethod, request.getMethod());
        }*/

        // 时间区间
        //if (ObjectUtil.isNotEmpty(request.getStarttime()) && ObjectUtil.isNotEmpty(request.getEndtime())) {
        //    lambdaQueryWrapper.between(CommunityStatRecord::getCreatetime, request.getStarttime(), request.getEndtime());
        //}
        // * -1 已结束 结束时间小于当前时间
        // * 1 进行中 结束时间大于当前时间 && 开始时间小于等于当前时间
        // * 0 未开始 开始时间大于当前时间
        /*
        if (ObjectUtil.isNotEmpty(request.getRunningStatus())) {
            switch (request.getRunningStatus()) {
                case -1:
                    lambdaQueryWrapper.lt(CommunityStatRecord::getEndtime, currentDate);
                    break;
                case 0:
                    lambdaQueryWrapper.gt(CommunityStatRecord::getStarttime, currentDate);
                    break;
                case 1:
                    lambdaQueryWrapper.ge(CommunityStatRecord::getEndtime, currentDate).le(CommunityStatRecord::getStarttime, currentDate);
                    break;
            }
        }*/

        lambdaQueryWrapper.orderByDesc(CommunityStatRecord::getCreateTime);
        List<CommunityStatRecord> communityStatRecords = dao.selectList(lambdaQueryWrapper);
        // 添加活动状态
        List<CommunityStatRecordPageDataResponse> communityStatRecordResponses = new ArrayList<>();

        communityStatRecords.stream().forEach(x->{
            CommunityStatRecordPageDataResponse communityStatRecordResponse = new CommunityStatRecordPageDataResponse();
            BeanUtils.copyProperties(x, communityStatRecordResponse);
            //communityStatRecordResponse.setRunningStatus(x.getStarttime(), x.getEndtime(), currentDate);
            communityStatRecordResponses.add(communityStatRecordResponse);
        });
        return communityStatRecordResponses;
    }

    /**
    * 修改小区分红统计排名表
    * @param request
    * @return
    */
    @Override
    public boolean update(CommunityStatRecordSaveRequest request) {
        if (ObjectUtil.isNull(request.getId()) || request.getId() < 1) {
            throw new AppException(CommonResultCode.VALIDATE_FAILED, "请传入小区分红统计排名表ID");
        }
        getByIdException(request.getId());
        CommunityStatRecord communityStatRecord = new CommunityStatRecord();
        BeanUtils.copyProperties(request, communityStatRecord);
        return updateById(communityStatRecord);
    }

    private CommunityStatRecord getByIdException(Integer id) {
        CommunityStatRecord communityStatRecord = getById(id);
        if (ObjectUtil.isNull(communityStatRecord)) {
            throw new AppException("小区分红统计排名表不存在");
        }
        if (communityStatRecord.getIsDel()) {
            throw new AppException("小区分红统计排名表不存在");
        }
        return communityStatRecord;
    }

    /**
    * 批量删除
    * @param idList
    * @return
    */
    @Override
    public boolean batchDelete(List<Integer> idList) {
    LambdaUpdateWrapper<CommunityStatRecord> luw = Wrappers.lambdaUpdate();
        luw.set(CommunityStatRecord::getIsDel, true);
        luw.in(CommunityStatRecord::getId, idList);
        boolean update = update(luw);
        if (!update) {
            throw new AppException(CommonResultCode.ERROR.setMessage("批量删除小区分红统计排名表失败"));
        }
        return true;
    }

    /**
    * 查询溯源套餐表详情
    * @param id
    * @return
    */
    @Override
    public CommunityStatRecordDetailResponse getDetails(Integer id) {
        LambdaQueryWrapper<CommunityStatRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(CommunityStatRecord::getIsDel, 0);
        lqw.last("limit 1");
        CommunityStatRecord communityStatRecord = this.getOne(lqw);
        if (ObjectUtil.isNotNull(communityStatRecord)) {
            CommunityStatRecordDetailResponse response = new CommunityStatRecordDetailResponse();
            BeanUtils.copyProperties(communityStatRecord, response);
            return response;
        }
        return null;
    }

    @Override
    public List<CommunityStatRecord> getRankingList(Integer statId) {
        LambdaQueryWrapper<CommunityStatRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(CommunityStatRecord::getIsDel, false);
        lqw.eq(CommunityStatRecord::getStatId, statId);
        lqw.orderByAsc(CommunityStatRecord::getRankingNum);
        return list(lqw);
    }

}

